package square;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;

public class UI {
    // 创建一个私有的静态AudioPlayer对象，用于播放音频
    private static AudioPlayer audio = new AudioPlayer();
    // 创建一个私有的静态JFrame对象，表示应用程序的主窗口
    private static JFrame frame = new JFrame();
    // 创建一个私有的静态CardLayout对象，用于管理多个面板之间的切换
    private static CardLayout cardLayout = new CardLayout();
    // 创建一个私有的静态JPanel对象，使用CardLayout作为其布局管理器
    private static JPanel cardPanel = new JPanel(cardLayout);


    // 难度标签
    private static JLabel difficultyLabel;
    public static final int EASY = 0;
    public static final int NORMAL = 1; // 修正拼写错误：NURMAL 应为 NORMAL
    public static final int DIFFICU = 2;


    public static void main(String[] args) {
        // 创建窗口对象
        frame.setSize(535, 605);//设置窗口大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭动作
        frame.setLocationRelativeTo(null);//窗口居中显示屏幕上
        frame.setResizable(false);//不能通过鼠标放大缩小窗口

        // 创建两个面板，一个用于显示背景和按钮，另一个用于显示游戏面板
        JPanel startPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(new ImageIcon("src/images/background.png").getImage(), 0, 0, getWidth(), getHeight(), this);
            }
        };
        startPanel.setLayout(null);

        // 创建并添加开始游戏按钮
        JButton button = new JButton("开始游戏");
        button.setBounds(70, 400, 150, 37);
        button.setBackground(Color.BLUE);
        button.setBorderPainted(false);
        startPanel.add(button);

        // 添加模式切换按钮
        JButton modeButton = new JButton("切换模式");
        modeButton.setBounds(70, 450, 150, 37);
        modeButton.setBackground(Color.blue);
        startPanel.add(modeButton);

        // 添加难度标签
        difficultyLabel = new JLabel("当前难度: 简单");
        difficultyLabel.setBounds(70, 350, 200, 50); // 位置可以根据需要调整
        startPanel.add(difficultyLabel);

        // 添加游戏面板
        Tetris panel = new Tetris();
        cardPanel.add(startPanel, "start");
        cardPanel.add(panel, "game");

        // 定义菜单栏、菜单、子菜单
        JMenuBar menu = new JMenuBar();
        frame.setJMenuBar(menu);
        JMenu jm = new JMenu("选项");
        menu.add(jm);
        JMenuItem jm1 = new JMenuItem("返回主界面");
        JMenuItem jm2 = new JMenuItem("重新开始");
        jm.add(jm1);
        jm.add(jm2);
        jm1.addActionListener(e -> {
            cardLayout.show(cardPanel, "start");
            audio.stop();
            panel.reset();
        });
        jm2.addActionListener(e -> {
            panel.reset();
            // panel.start();
        });

        // 音乐菜单
        JMenu musicMenu = new JMenu("音乐");
        menu.add(musicMenu);
        JMenuItem jm3 = new JMenuItem("暂停");
        JMenuItem jm4 = new JMenuItem("继续");
        jm3.addActionListener(e -> stopmusic());
        jm4.addActionListener(e -> startmusic());
        musicMenu.add(jm3);
        musicMenu.add(jm4);

        // 难度选择
        JPopupMenu modeMenu = new JPopupMenu();
        JMenuItem mode1 = new JMenuItem("简单模式");
        JMenuItem mode2 = new JMenuItem("普通模式");
        JMenuItem mode3 = new JMenuItem("困难模式");
        modeMenu.add(mode1);
        modeMenu.add(mode2);
        modeMenu.add(mode3);
        
        modeButton.addActionListener(e -> {
            modeMenu.show(modeButton, 0, modeButton.getHeight());
        });

        // 难度选择事件
        mode1.addActionListener(e -> {
            JOptionPane.showMessageDialog(frame, "已选择简单模式");
            updateDifficultyLabel(UI.EASY); // 更新难度
        });
        mode2.addActionListener(e -> {
            JOptionPane.showMessageDialog(frame, "已选择普通模式");
            updateDifficultyLabel(UI.NORMAL); // 更新难度
        });
        mode3.addActionListener(e -> {
            JOptionPane.showMessageDialog(frame, "已选择困难模式");
            updateDifficultyLabel(UI.DIFFICU); // 更新难度
        });

        // 鼠标经过小手图标
        Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
        button.setCursor(cursor);
        jm.setCursor(cursor);
        jm1.setCursor(cursor);
        jm2.setCursor(cursor);
        jm3.setCursor(cursor);
        jm4.setCursor(cursor);
        modeMenu.setCursor(cursor);
        musicMenu.setCursor(cursor);

        // 将cardPanel添加到frame中
        frame.add(cardPanel);

        // 为按钮添加一个动作监听器，当按钮被点击时执行以下操作
        button.addActionListener(e -> {
            cardLayout.show(cardPanel, "game");
            panel.start();
            audio.loop("src/audio/bg.wav");
        });

        // 设置自定义UI 鼠标经过按钮不变
        button.setUI(new BasicButtonUI() {
            @Override
            protected void installDefaults(AbstractButton b) {
                super.installDefaults(b);
                //button.setRolloverEnabled(false);
            }
        });


        frame.setVisible(true);
    }

    // 更新难度标签
    private static void updateDifficultyLabel(int difficulty) {
        switch (difficulty) {
            case EASY:
                difficultyLabel.setText("当前难度: 简单");
                break;
            case NORMAL:
                difficultyLabel.setText("当前难度: 普通");
                break;
            case DIFFICU:
                difficultyLabel.setText("当前难度: 困难");
                break;
        }
    }
    
    // 停止音乐
    private static void stopmusic() {
        audio.stop();
    }

    private static void startmusic() {
        audio.start();
    }
    
}
